package com.woniuxy.his.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.his.common.CommonUtils;
import com.woniuxy.his.common.JwtUtils;
import com.woniuxy.his.common.Result;
import com.woniuxy.his.entity.Doctor;
import com.woniuxy.his.mapper.DoctorMapper;
import com.woniuxy.his.service.IDoctorService;
import com.woniuxy.his.service.ILoginService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * <p>
 * 医生表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-08-25
 */
@RestController
//@RequestMapping("/his/doctor")
@CrossOrigin("*")
@Slf4j

public class DoctorController {


    public String code;
    @Resource
    private DoctorMapper doctorMapper;

    @Resource
    private ILoginService loginService;

    @Resource
    private IDoctorService doctorService;

//    @Resource
//    private RedissonClient redissonClient;

    @GetMapping("/email")

    public Result email(@RequestHeader("jwt") String email) throws MessagingException {
        Claims parse = JwtUtils.parse(email);
        System.out.println(parse);

        log.info("邮箱为:{}",email);
        code= CommonUtils.getRandomStr();
//        RBucket<Object> bucket = redissonClient.getBucket("code");
//        bucket.set(code);
        log.info("验证码为：{}", this.code);
        QueryWrapper<Doctor> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email);
        Doctor doctor = doctorMapper.selectOne(wrapper);

        if(null!=doctor){
            log.info("医生为",doctor);
            CommonUtils.sendMail(email,"【HIS】重置验证码","您的HIS系统重置验证码为"+ this.code);
            return Result.success("2000","找到邮箱"+email,doctor);
        }
        return Result.fail("400","邮箱"+email+"未注册his系统",null);
    }

    @GetMapping("/reset")
    public Result reset(Integer doctorId,String activeCode) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        log.info("医生id为{}",doctorId);
//        RBucket<Object> bucket = redissonClient.getBucket("code");
//        String code=(String)bucket.get();
        log.info("验证码{}",code);
//        改doctorId的密码,加密后的123
        String md5 = CommonUtils.md5("123");
        QueryWrapper<Doctor> wrapper = new QueryWrapper<>();
        wrapper.eq("doctor_id",doctorId);
        Doctor doctor = doctorMapper.selectOne(wrapper);
        if(null!=doctor&&code.equals(activeCode)){
            doctor.setPassword(md5);
            doctorMapper.updateById(doctor);
            return Result.success("200","初始化密码成功",doctor);
        }else if(!code.equals(activeCode)){
            return Result.fail("400","验证码不对",null);
        }
        return Result.fail("400","无该医生存在，初始化密码失败",null);
    }


    @GetMapping("/findDoctorByDepId")

    public Result findDoctorByDepId(Integer depId){
        log.info("传来的科室id为{}",depId);
        QueryWrapper<Doctor> wrapper = new QueryWrapper<>();
        wrapper.eq("dep_id",depId);
        List<Doctor> doctorList = doctorMapper.selectList(wrapper);
        if(null!=doctorList){
            return Result.success("200","查询科室id为"+depId+"”的医生们成功",doctorList);
        }
        return Result.fail("400","查询科室id为"+depId+"”的医生们失败",null);
    }

    @GetMapping("/findDoctorsByArrangeTime")
    public Result findDoctorsByDate(String arrangeTime,Integer current,Integer size){
        log.info("查找{}上班的医生，当前页{},页容量{}",arrangeTime);
        IPage<Doctor> doctorsByPage = doctorService.findDoctorsByArrangeTime(arrangeTime, current, size);
        if (null==doctorsByPage) {
            return Result.fail("400","未找到医生","");
        }else{
            log.info("找到排班信息并返回");
            return Result.success("200","找到排班医生",doctorsByPage);
        }
    }

}

